//
//  HWMyAssetsController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/1/19.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView
class HWMyAssetsController: HWBaseViewController {
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var listContainerView: JXSegmentedListContainerView!
    var lineView:UIView!
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
    }

}

extension HWMyAssetsController {
    private func initUI() -> Void {
        
        navigationView.titleLabel.text = "我的资产"
        navigationView.backgroundColor = .white
        navigationView.backButton.setImage(UIImage(named: "fanhui"), for: .normal)
        view.backgroundColor = .colorWithHexString(hexString: "#F7F7F7")
        
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = ["游戏","直播"]
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = UIColor.colorWithHexString(hexString: "#666666")
        segmentedDataSource.titleSelectedColor = UIColor.colorWithHexString(hexString: "#333333")
        segmentedDataSource.titleNormalFont = UIFont.systemFont(ofSize: 14*widthScale)
        segmentedDataSource.titleSelectedFont = UIFont.boldSystemFont(ofSize: 16*widthScale)
        segmentedDataSource.isItemSpacingAverageEnabled = true
        segmentedDataSource.itemSpacing = 0
        segmentedDataSource.itemWidth = screenWidth/2
        segmentedView.dataSource = segmentedDataSource
        
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorColor = .colorWithHexString(hexString: "#FFB60A")
        indicator.lineStyle = .normal
        indicator.indicatorHeight = 2*widthScale
        indicator.indicatorWidth = 130*widthScale
        segmentedView.backgroundColor = .white
        segmentedView.indicators = [indicator]

        view.addSubview(self.segmentedView)
        
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        view.addSubview(listContainerView)
        
        segmentedView.listContainer = listContainerView
        segmentedView.delegate = self
        
        lineView = UIView()
        lineView.backgroundColor = .colorWithHexString(hexString: "F7F7F7")
        view.addSubview(lineView)
    }
    
    private func initFrame() -> Void {
        segmentedView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(navigationView.snp.bottom).offset(2*widthScale)
            make.height.equalTo(40*widthScale)
        }
        
        listContainerView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(segmentedView.snp.bottom).offset(0)
            make.bottom.equalTo(0)
        }
        
        lineView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(1)
            make.height.equalTo(40*widthScale)
            make.centerY.equalTo(segmentedView)
        }
    }
}

extension HWMyAssetsController:JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate{
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }

    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        
        let assetsController = HWAssetsViewController()
        
        let liveAssets = HWLiveAssetsController()
//        pageController.tagId = self.tagModelArr?[index].id ?? ""
//        pageView.models = self.giftModels?[index].list
//        pageView.didSelectGift = {
//            mod in
//            self.didSelectGift?(mod)
//        }
//        pageArr.remove(at: index)
//        pageArr.insert(pageView, at: index)
        if index == 0 {
            return assetsController as! JXSegmentedListContainerViewListDelegate
        }else {
            return liveAssets
        }
        
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
//        for (idx,_) in self.giftModels!.enumerated() {
//
//        }
//        for arr in self.giftModels?[index].list ?? [] {
//            for mod in arr {
//                mod.selected = false
//            }
//        }
//        pageArr[index].models = self.giftModels?[index].list
    }
}
